<template>
  <el-container class="richtext-select panel-block">
    <el-main>
      <div class="sa-flex sa-row-between sa-m-b-24">
        <div class="sa-flex">
          <el-input placeholder="请输入查询内容" v-model="searchtext" clearable>
            <template #append>
              <el-button @click="openFilter">搜索</el-button>
            </template>
          </el-input>
        </div>
        <el-button v-auth="'shop.data.richtext.add'" icon="Plus" type="primary" @click="addRow"
          >添加</el-button
        >
      </div>
      <template v-if="table.data.length > 0">
        <el-table class="sa-table sa-m-b-16" :data="table.data" row-key="id" stripe>
          <el-table-column prop="id" label="ID" min-width="100"></el-table-column>
          <el-table-column label="标题" min-width="100">
            <template #default="scope">
              <span class="sa-table-line-1">{{ scope.row.title || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="172">
            <template #default="scope">
              {{ scope.row.create_time || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="更新时间" min-width="172">
            <template #default="scope">
              {{ scope.row.update_time || '-' }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" min-width="100">
            <template #default="scope">
              <el-button class="is-link" type="primary" @click="modalCallBack(scope.row)"
                >选择</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="sa-flex sa-row-right">
          <sa-pagination :pageData="pageData" @updateFn="getData" />
        </div>
      </template>
      <template v-if="table.data.length == 0 && !loading">
        <sa-empty></sa-empty>
      </template>
    </el-main>
  </el-container>
</template>

<script>
  export default {
    name: 'RichtextSelect',
  };
</script>

<script setup>
  import { onMounted, reactive, ref } from 'vue';
  import { api } from '../data.service';
  import { useModal } from '@/sheep/hooks';
  import { usePagination } from '@/sheep/hooks';
  import RichtextEdit from './edit.vue';
  const props = defineProps(['modal']);
  const emit = defineEmits(['modalCallBack']);
  const { pageData } = usePagination();
  // 列表
  const table = reactive({
    data: [],
    selected: {},
  });
  const loading = ref(true);
  const searchtext = ref('');
  async function openFilter() {
    let search = {
      keyword: [searchtext.value],
    };
    const { data } = await api.richtext.select({
      page: pageData.page,
      list_rows: pageData.list_rows,
      search,
    });

    table.data = data.data;
    pageData.page = data.current_page;
    pageData.list_rows = data.per_page;
    pageData.total = data.total;
  }
  // 获取
  async function getData() {
    loading.value = true;
    const { data } = await api.richtext.select({
      page: pageData.page,
      list_rows: pageData.list_rows,
    });
    table.data = data.data;
    pageData.page = data.current_page;
    pageData.list_rows = data.per_page;
    pageData.total = data.total;
    loading.value = false;
  }
  function addRow() {
    useModal(
      RichtextEdit,
      { title: '新建', type: 'add' },
      {
        confirm: () => {
          getData();
        },
      },
    );
  }
  function modalCallBack(e) {
    emit('modalCallBack', { event: 'confirm', data: e });
  }

  onMounted(() => {
    getData();
  });
</script>
<style lang="scss" scoped>
  .richtext-select {
    .el-main {
      :deep() {
        .el-table__header {
          .el-table-column--selection {
            .el-checkbox {
              display: none;
            }
          }
        }
      }
    }
  }
</style>
